#!/bin/bash
# DJVU_Monochrome_Center_1.sh ( 22 ) : This script , by Frank Trampe , crops and recodes specified pages from a specified input DJVU file to specified dimensions . The dimensions of the output page can be smaller than , larger than , or equal to the corresponding dimensions of the input page , but no pixel dimension can be greater than 32766 . The program's output locations are fixed . The intention is that this script ought to be part of a larger workflow that stores page files in a temporary directory before binding them into final output files and deleting them .
# This script is not capable of intelligently modifying the djvu file . It extracts page images , modifies them , and encodes them into new djvu files . This script is designed solely for monochrome image processing , and preserves no colouring or shading information . One can adjust the image thresholds by modifying the script so that it takes colour output from ddjvu and pipes it through ppmtopgm and pgmtopbm according to certain options . The script is designed for cropping djvu files that are already in monochrome format , though , not for converting gray and colour djvu files .
# The first argument is the input file . The second argument is the starting page number . The third argument is the ending page number . The fourth argument is the output page width . The fifth argument is the output page height . The sixth argument is the output resolution , which affects not pixel count but physical dimensioning .
# We change to the desired output directory and specify the input file .
# DJVU_Monochrome_Center_1.sh Input_1.djvu 1 128 5100 6600 600
# We then need to bind the files .
# djvm -c ./Output_1.C.djvu ./Page_*.Cr.djvu ; djvm -c ./Output_1.Cl.djvu ./Page_*.Clr.djvu ; djvm -c ./Output_1.L.djvu ./Page_*.Lr.djvu ;
# Cr represents the converted files that can be removed . Clr represents the cleaned converted files that can be removed . Lr represents the lossy converted files that can be removed . For a multi-task workflow , it seems most helpful to change to a temporary directory for all of the internal steps in a conversion , extracting and processing page images there and writing the final , joined files back to the original directory .
# This script requires that the PBM_Processor_1 and PBM_Info_1 utilities be in the system's path . It requires the ddjvu decompressor and the cjb2 compressor , which are included in the djvulibre-bin package .
File1=$1 ;
Start1=$2 ;
End1=$3 ;
Width2=$4 ;
Height2=$5 ;
for Page1 in `seq $Start1 $End1`
do
  Loc1="`Number_Crop_1 4 $Page1`" ;
  File5="Page_$Loc1.Cr.djvu" ;
  File6="Page_$Loc1.Clr.djvu" ;
  File7="Page_$Loc1.Lr.djvu" ;
  ddjvu -format=pbm -mode=black -page=$Page1 $File1 Temp_1.pbm ;
  Width1=`PBM_Info_1 --Width < Temp_1.pbm` ;
  Height1=`PBM_Info_1 --Height < Temp_1.pbm` ;
  if [[ $Width1 = $Width2 ]]
  then
    let "XOffset1 = ( ( Width1 - Width2 ) / 2 )" ;
    let "XOffset2 = 0" ;
    XTotal1=$Width2 ;
    XTotal2=$Width2 ;
  fi
  if [[ $Width1 > $Width2 ]]
  then
    let "XOffset1 = ( ( Width1 - Width2 ) / 2 )" ;
    let "XOffset2 = 0" ;
    XTotal1=$Width2 ;
    XTotal2=$Width2 ;
  fi
  if [[ $Width1 < $Width2 ]]
  then
    let "XOffset1 = 0" ;
    let "XOffset2 = ( ( Width2 - Width1 ) / 2 )" ;
    XTotal1=$Width1 ;
    XTotal2=$Width1 ;
  fi
  if [[ $Height1 = $Height2 ]]
  then
    let "YOffset1 = ( ( Height1 - Height2 ) / 2 )" ;
    let "YOffset2 = 0" ;
    YTotal1=$Height2 ;
    YTotal2=$Height2 ;
  fi
  if [[ $Height1 > $Height2 ]]
  then
    let "YOffset1 = ( ( Height1 - Height2 ) / 2 )" ;
    let "YOffset2 = 0" ;
    YTotal1=$Height2 ;
    YTotal2=$Height2 ;
  fi
  if [[ $Height1 < $Height2 ]]
  then
    let "YOffset1 = 0" ;
    let "YOffset2 = ( ( Height2 - Height1 ) / 2 )" ;
    YTotal1=$Height1 ;
    YTotal2=$Height1 ;
  fi
  echo $Page1
  echo "PBM_Processor_1 --ManualDim2 1 --Width2 $Width2 --Height2 $Height2 --XOffset1 $XOffset1 --YOffset1 $YOffset1 --XOffset2 $XOffset2 --YOffset2 $YOffset2 --XTotal1 $XTotal1 --YTotal1 $YTotal1 --XTotal2 $Width2 --YTotal2 $Height2 < Temp_1.pbm > Temp_2.pbm" ;
PBM_Processor_1 --ManualDim2 1 --Width2 $Width2 --Height2 $Height2 --XOffset1 $XOffset1 --YOffset1 $YOffset1 --XOffset2 $XOffset2 --YOffset2 $YOffset2 --XTotal1 $XTotal1 --YTotal1 $YTotal1 --XTotal2 $Width2 --YTotal2 $Height2 < Temp_1.pbm > Temp_2.pbm ;
  cjb2 -dpi $6 Temp_2.pbm $File5 ;
  cjb2 -dpi $6 -clean Temp_2.pbm $File6 ;
  cjb2 -dpi $6 -lossy Temp_2.pbm $File7 ;
  rm ./Temp_1.pbm ;
  rm ./Temp_2.pbm ;
done

